package cn.rentaotao.spring.mapper;

import cn.rentaotao.spring.data.IUser;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.lang.Nullable;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * @author rtt
 * @date 2022/5/26 14:01
 */
@Repository
public class DefaultUserMapper implements UserMapper, InitializingBean {

    private Map<String, IUser> users;

    @Override
    @Nullable
    public IUser getUser(String name) {

        return users.get(name);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

        users = new HashMap<>();

        String rtt = "rtt";
        IUser rttUser = new IUser();
        rttUser.setUsername(rtt);
        rttUser.setPassword(encoder.encode("03121994"));
        rttUser.setRoles(Collections.singletonList("ROLE_USER"));
        users.put(rtt, rttUser);

        String admin = "admin";
        IUser adminUser = new IUser();
        adminUser.setUsername(admin);
        adminUser.setPassword(encoder.encode("admin"));
        adminUser.setRoles(Arrays.asList("ROLE_ADMIN", "ROLE_USER"));
        users.put(admin, adminUser);
    }
}
